/*
 * @Description: http 封装
 * @Author: LiuYan
 * @Date: 2021-12-05 13:54:34
 * @LastEditTime: 2023-01-10 15:11:03
 * @LastEditors: LiuYan
 * @Reference:
 */
import axios from "axios";
import router from "../router/index";
import * as app from "../config/index";
import { userStore } from "../store/user";
const { HTTP, APP } = app.default;
//请求拦截
axios.interceptors.request.use(function (config) {
  if (process.env.NODE_ENV === "production") config.baseURL = HTTP.cross_domain;
  return config;
});
//响应拦截
axios.interceptors.response.use((res) => {
  const _res = res.data;
  if (_res.code === 401 && APP.isLogin) {
    router.replace("/login");
  }
  if (_res.code === 500) {
    console.error("500");
  }
  return _res;
});
const fetch = ({
  url,
  method = "GET",
  params,
  data = {},
  headers = {
    "content-type": "application/json",
  },
  token = true,
  ...other
}) => {
  const options = {
    url,
    method,
    data,
    params,
    headers,
    ...other,
  };
  if (token) {
    const user = userStore();
    options.headers["Authorization"] = user.token;
  }
  return new Promise((resolve, reject) => {
    axios(options)
      .then((res) => {
        resolve(res);
      })
      .catch((err) => {
        const res = err.response;
        if (res.status === 500) {
          console.error("500");
        }
        reject(err);
      });
  });
};
export default fetch;
